/**
 *  存放 用户 数据
 * **/
import * as types from '../mutation-type';
import { MessageBox,Message } from 'element-ui';
// initial state
const state = {
  GloableToolTopbody: 'dd',
  stateAllSubType: [],
  stateAllSubTypeArr: [],
  stateAllSubTypeObj: {
    pocID: 1,  //POC终端  1 1
    GotaID: 1,  //Gota终端 1 2
    QchatID: 1,  //Qchat终端 1 3
    DMRID: 1,  //DMR终端 1 4

    SIPZD: 1,  //SIP终端 5 1
    RTMPSPY: 1,  //RTMP视频源 5 2
    BKQSDK: 1,  //布控球(SDK) 5
    BKQGBT28181: 1,  //布控球(GBT28181) 5
    WRJGBT28181: 1,  //无人机(GBT28181) 5
    HWSXT: 1,  //红外摄像头 5

    MNLWG: 1,  //模拟类网关 10 1
    DMRCT: 1,  //DMR 车台 10

    SIPZD201: 1,  //SIP终端 20 1


    DNRZJT: 1,  //DMR中继台21 3
    SPWG: 1,  //视频网关 21 1
    DMRWG: 1,  //DMR网关 21 2
    PSIPWG: 1,  //PSIP网关 21 2
    PDTWG: 1,  //PDT网关 21 2
    PDTZG: 1,  //PDT终端 1 4

  },
  stateAllSubTypeArr2: [1,2,3,4,5,6,1,1,1,1,1,1,1,1,1,1,4],
  userInfo: {
    name: '调度员：小峰',
    scsFleetId: '',
    account: '',
    userId: '',
    license: [],
    roles: [71, 54],
    toolTopbody: '',
    userRoles: [],
  },

  skuDatas:[
    {
      id:1,
      photo: '',
      pingpai: '2品牌',
      spm: '天然乳胶枕(波浪型)',
      guige: '60x40+10/12cm',
      danStatus: '乳胶枕',
      leimu: '乳胶枕',
      danwei: '个',
      cpsx: '面辅料',
      bzj: '99.339',
      bzzk: '100',
      jdrq: '2020-09-14',
      jdzt: '停用中',
    },      {
      id:2,
      photo: '',
      pingpai: '2品牌',
      spm: '天然乳胶枕(波浪型)',
      guige: '60x40+10/12cm',
      danStatus: '乳胶枕',
      leimu: '乳胶枕',
      danwei: '个',
      cpsx: '面辅料',
      bzj: '99.339',
      bzzk: '100',
      jdrq: '2020-09-14',
      jdzt: '使用中',
    },      {
      id:3,
      photo: '',
      pingpai: '2品牌',
      spm: '天然乳胶枕(波浪型)',
      guige: '60x40+10/12cm',
      danStatus: '乳胶枕',
      leimu: '乳胶枕',
      danwei: '个',
      cpsx: '面辅料',
      bzj: '99.339',
      bzzk: '100',
      jdrq: '2020-09-14',
      jdzt: '使用中',
    },     {
      id:4,
      photo: '',
      pingpai: '2品牌',
      spm: '天然乳胶枕(波浪型)',
      guige: '60x40+10/12cm',
      danStatus: '乳胶枕',
      leimu: '乳胶枕',
      danwei: '个',
      cpsx: '面辅料',
      bzj: '99.339',
      bzzk: '100',
      jdrq: '2020-09-14',
      jdzt: '使用中',
    },
  ],
};

// getters
const getters = {
  userInfo(state) {
    return state.name;
  },
};

// actions
// 异步方法用actions
// actions不能直接修改全局变量，需要调用commit方法来触发mutation中的方法
const actions = {
  login(context, payload) {
    context.commit('login', payload);
  },
  ChangeGloableToolTopbody(context, payload) {
    context.commit('ChangeGloableToolTopbody', payload);
  },
  logout(context) {
    context.commit('logout');
  },
  saveGlobalUserInfo(context, payload) {
    context.commit('saveGlobalUserInfo', payload);
  },
};

// mutations
const mutations = {
  [types.SET_NUM](state, all) { //设置参数
    state.all = all;
  },
  login(state, payload) {
    // state.name = payload
    // localStorage.setItem(user_key, JSON.stringify(payload));
    //保存 用户登录时间
    let timespan = Date.parse(new Date());
    // localStorage.setItem(user_time, timespan);
  },
  logout(state) {
    state.userInfo = null;
    // localStorage.setItem(user_key, null);
  },
  ChangeGloableToolTopbody(state, payload) {
    state.GloableToolTopbody = payload;
    // localStorage.setItem(user_key, null);
  },
  [types.saveGlobalUserInfo](state, payload) {
    // FleetIdControllerApi.getfleetId({
    //   userId: payload,
    // }).then(res => {
    //   // 返回集团ID
    //   // this.scsFleetId = res.obj[0].fid;
    //   // this.user.userId = res.obj[0].user_id;
    //   state.userInfo.scsFleetId = res.obj[0].fid;
    //   state.userInfo.userId = res.obj[0].user_id;
    //
    // }).catch(err => {
    //
    // });
    // localStorage.setItem(user_key, null);
  },

  //数据管理
  addSkuDatas(state, payload) {
    state.skuDatas.push(payload);
  },
  updateSkuDatas(state, id) {
    for (let i=0;i<state.skuDatas.length;i++){
      if (id ==state.skuDatas[i].id){
        if (state.skuDatas[i].danStatus =='已确认'){
          Message.success("该订单已经确认");
        }else {
          state.skuDatas[i].danStatus = "已确认";
          Message.success("操作成功");
        }
      }
    }
  },
  deleteSkuDatas(state, id) {
    for (let i=0;i<state.skuDatas.length;i++){
      if (id ==state.skuDatas[i].id){
        state.skuDatas.splice(i,1);
        Message.success("删除成功");
      }
    }
  },

};

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations,
};
